//
//  ZRVisitorView.swift
//  weiboDemo
//
//  Created by 张睿 on 16/7/19.
//  Copyright © 2016年 张睿. All rights reserved.
//

import UIKit
import SnapKit

class ZRVisitorView: UIView {
    // 声明一个闭包
    var closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
// MARK: - 供外界调用
    func setupVisitorViewInfo(imgName: String?, title: String?){
        // 判断imgName 和title是否为nil
        // 如果不为nil  代表设置的为 消息 发现 我
        //因为我们下载设置显示的页面就是主页,所以下载只需考虑 消息 发现 我
        if let img = imgName, let tit = title{
            houseImageView.image = UIImage(named: img)
            messageLabel.text = tit
            feedImageView.hidden = true
        }else{
            setupAnim()
        }
    }
//MARK: - 设置圆圈动画
    func setupAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置toValue
        anim.toValue = M_PI * 2
        // 设置时间
        anim.duration = 10
        // 设置重复次数
        anim.repeatCount = MAXFLOAT
        // 当切换控制器或者程序退到后台 默认动画会停止
        anim.removedOnCompletion = false
        // 添加动画
        feedImageView.layer.addAnimation(anim, forKey: nil)
    }
    
//MARK: - 设置视图
    func setupUI(){
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(regButton)
        
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        houseImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 230, height: 36))
            make.centerX.equalTo(self)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel)
        }
        regButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
    }
    
    func didLoginClick(){
        
        closure?()
    }
    
    //common_button_white_disable
    //MARK: - 懒加载
   private lazy var feedImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
   private lazy var maskImageView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
   private lazy var houseImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.font = UIFont.systemFontOfSize(14)
        lab.textColor = UIColor.grayColor()
        lab.numberOfLines = 0
        
        return lab
    }()
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "didLoginClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return btn
    }()
    private lazy var regButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "didLoginClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return btn
    }()
}
